<template>
  <div class="top-category">
    <div class="container">
      <xtx-bread sparator=">">
        <xtx-bread-item to="/">主页</xtx-bread-item>
        <xtx-bread-item>{{ cate.name }}</xtx-bread-item>
      </xtx-bread>

      <!-- 轮播图 -->
      <xtx-carousel :sliders="sliders" :autoplay="true" style="height: 500px" />

      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="sub in cate.children" :key="sub.id">
            <a href="javascript:;">
              <img :src="sub.picture" />
              <p>{{ sub.name }}</p>
            </a>
          </li>
        </ul>
      </div>

      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="sub in cate.children" :key="sub.id">
        <div class="head">
          <h3>- {{ sub.name }} -</h3>
          <XtxMore to="/" />
        </div>
        <div class="body">
          <GoodsItem v-for="good in sub.goods" :key="good.id" :good="good" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import xtxCarousel from '@/components/xtx-carousel.vue'
import { findTopCategory } from '@/api/category'
import { findBanner } from '@/api/home'
import { ref, onMounted } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import GoodsItem from './components/goods-item.vue'
export default {
  components: { xtxCarousel, GoodsItem },
  name: 'TopCategory',
  setup () {
    const route = useRoute()
    const sliders = ref([])
    const cate = ref({})

    const loadCategoryList = (id) => {
      // 获取轮播图信息
      findBanner().then(data => {
        sliders.value = data.result
      })

      // 获取当前一级类目的全部分类
      findTopCategory(id).then(data => {
        cate.value = data.result
      })
    }
    // 页面创建时执行（直接从路由中获取id）
    onMounted(() => {
      loadCategoryList(route.params.id)
    })
    // 在路由跳转之后更新之前自动执行
    // beforeEach((to,from,next)=>{})
    onBeforeRouteUpdate((to) => {
      // to指代的是目标路由对象 to拿到最新的路由参数id
      // 使用最新id获取数据
      loadCategoryList(to.params.id)
    })

    return { sliders, cate }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
</style>
<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
</style>
